﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using TrainingManagement.Models;

namespace TrainingManagement.Controllers
{
    public class ExpectKnowledgeTypeAPIController : ApiController
    {
        private trainingDBEntities db = new trainingDBEntities();

        // GET api/ExpectKnowledgeTypeAPI
        public IEnumerable<tblExpectKnowledgeType> GettblExpectKnowledgeTypes()
        {
            return db.tblExpectKnowledgeTypes.AsEnumerable();
        }

        // GET api/ExpectKnowledgeTypeAPI/5
        public tblExpectKnowledgeType GettblExpectKnowledgeType(int id)
        {
            tblExpectKnowledgeType tblexpectknowledgetype = db.tblExpectKnowledgeTypes.Single(t => t.Id == id);
            if (tblexpectknowledgetype == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return tblexpectknowledgetype;
        }

        // PUT api/ExpectKnowledgeTypeAPI/5
        public HttpResponseMessage PuttblExpectKnowledgeType(int id, tblExpectKnowledgeType tblexpectknowledgetype)
        {
            if (ModelState.IsValid && id == tblexpectknowledgetype.Id)
            {
                db.tblExpectKnowledgeTypes.Attach(tblexpectknowledgetype);
                db.ObjectStateManager.ChangeObjectState(tblexpectknowledgetype, EntityState.Modified);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/ExpectKnowledgeTypeAPI
        public HttpResponseMessage PosttblExpectKnowledgeType(tblExpectKnowledgeType tblexpectknowledgetype)
        {
            if (ModelState.IsValid)
            {
                db.tblExpectKnowledgeTypes.AddObject(tblexpectknowledgetype);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, tblexpectknowledgetype);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = tblexpectknowledgetype.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/ExpectKnowledgeTypeAPI/5
        public HttpResponseMessage DeletetblExpectKnowledgeType(int id)
        {
            tblExpectKnowledgeType tblexpectknowledgetype = db.tblExpectKnowledgeTypes.Single(t => t.Id == id);
            if (tblexpectknowledgetype == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.tblExpectKnowledgeTypes.DeleteObject(tblexpectknowledgetype);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, tblexpectknowledgetype);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}